const {
  request
} = require("../../utils/index.js")
// pages/called/called.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:false,  //搜索框收缩
    resultList:[],  //活动组织列表数据
    Eliteshare:{
      pageNum:1,  //页数
      pageSize:10,  //条数
      searchLabel:"on_line",  //on_line线上  off_line线下  collect收藏
      searchName:"",   //名称查询
    }
  },

  //线上
  on_line(e){
    this.setData({
      "Eliteshare.searchLabel":"on_line",
    })
    this.init();
  },
  //线下
  off_line(e){
    this.setData({
      "Eliteshare.searchLabel":"off_line",
    })
    console.log(this.data.Eliteshare.searchLabel)
    this.init();
  },
  //收藏
  collect(e){
    this.setData({
      "Eliteshare.searchLabel":"collect",
    })
    this.init();
  },
  //搜索框搜索
  result(e){
    this.setData({
      "Eliteshare.searchName":e.detail
    });
    this.init();
  },
  //搜索框收缩状态
  search(){
    this.setData({
      search:true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  //获得活动组织列表数据
  async init(){
    try {
      let that = this;
      let param = {
        url:"hw/portal/activity/page",
        method: "GET",
        data:{
          token:wx.getStorageSync('token'),
          ...this.data.Eliteshare
        },
        header:{
          "Content-Type":"application/json"
        }
      }
        let datainfo = await request(param);
        console.log(datainfo)
        if(datainfo.status == 100){
          that.setData({
            resultList:datainfo.data
          })
        }
    } catch (error) {
      console.log(error)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      Eliteshare:{
        pageNum:1,  //页数
        pageSize:10,  //条数
        searchLabel:"on_line",  //on_line线上  off_line线下  collect收藏
        searchName:"",   //名称查询
      }
    })
    this.init();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    wx.showNavigationBarLoading(); //显示加载
    //模拟加载  1秒
    let that = this;
    if(this.data.Eliteshare.pageNum>=this.data.resultList.lastPage){
      wx.showToast({
        title: '滑倒底了',
        icon:"none"
      })
      wx.hideNavigationBarLoading();  //停止加载
      return;
    }
    this.setData({
      "Eliteshare.pageNum":++this.data.Eliteshare.pageNum
    })
    let par = {
      url:"hw/portal/activity/page?token=" + wx.getStorageSync('token'),
      method:"GET",
      data:that.data.Eliteshare,
      header: {
        "Content-Type": "application/json"
      },
    }
    let datainfo = await request(par);
    if(datainfo.data != undefined){
      let lao = that.data.resultList.list //将老数据存入
      if(datainfo.data.list){
        let list = [...lao,...datainfo.data.list]  //新老数据拼接
        that.setData({
          resultList:datainfo.data,  //重新赋值
          "resultList.list": list
        })
      }
    }else{
      // 滑倒底了
    }
    wx.hideNavigationBarLoading(); //停止加载
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})